package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.edu.domain.vo.ChapterVo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edu.domain.EduChapter;
import com.ruoyi.edu.service.IEduChapterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author niruichen
 * @date 2020-12-22
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/edu/chapter" )
public class EduChapterController extends BaseController {

    private final IEduChapterService iEduChapterService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('edu:chapter:list')")
    @GetMapping("/list/{courseId}")
    public AjaxResult list(@PathVariable String courseId) {
        List<ChapterVo> list = iEduChapterService.getChapterVideoByCourseId(courseId);
        return AjaxResult.success(list);
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('edu:chapter:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iEduChapterService.getById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('edu:chapter:add')" )
    @Log(title = "章节" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduChapter eduChapter) {
        return toAjax(iEduChapterService.save(eduChapter) ? 1 : 0);
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('edu:chapter:edit')" )
    @Log(title = "章节" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduChapter eduChapter) {
        return toAjax(iEduChapterService.updateById(eduChapter) ? 1 : 0);
    }

    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('edu:chapter:remove')" )
    @Log(title = "章节" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}" )
    public AjaxResult remove(@PathVariable String id) {
        //查询章节底下是否有小结
        if(iEduChapterService.hasChildById(id)){
            return AjaxResult.error("该章节底下有小结,不允许删除");
        }
        return toAjax(iEduChapterService.removeById(id) ? 1 : 0);
    }
}
